package fichiers;


import Composite.*;
import principal.GhostGlassPane;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import principal.*;

public class MonJTree extends JTree implements Observer {

    private String nom;
    
    public MonJTree(TreeModel model,String nom) {
        super(model);
        this.nom=nom;
        GestionnaireDeSelection.getInstance().addObserver(this);
        Ecouteur ecouteur = new Ecouteur(this);
        this.addMouseMotionListener(ecouteur);
        this.addMouseListener(ecouteur);

    }
   
    
    public void update(Observable o, Object arg) {

        if(!GestionnaireDeSelection.getInstance().hasTheSelection(this)){
            
            this.setSelectionPath(null);
            
        }
    }

}

class Ecouteur extends MouseAdapter{

    private JTree arbre;
    private TreePath ancienPath;
    private Element repres;
    private GhostGlassPane glassPane = (GhostGlassPane) Main.fenetrePrincipal.getGlassPane();
    
    public Ecouteur(JTree arbre){
        
        this.arbre=arbre;
    }
    
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {

      TreePath destinationPath = this.arbre.getPathForLocation(e.getX(),e.getY());   
      if(destinationPath!=null && !destinationPath.equals(ancienPath)){

           MyDefaultMutableTreeNode newParent = (MyDefaultMutableTreeNode) destinationPath.getLastPathComponent();
           this.repres= (Element)newParent.getUserObject();
           this.ancienPath=destinationPath;
           BufferedImage im=(BufferedImage) this.repres.getVignette();
           glassPane.setImage(im);
           glassPane.setVisible(true);
      }
      
      if(destinationPath!=null && this.repres.estUnePhoto()){
                
                Point p2=Main.fenetrePrincipal.getMousePosition();
                glassPane.setPoint(p2);
                glassPane.repaint();
                
      }
      else{
                glassPane.setImage(null);
                glassPane.setVisible(false);
           }
       }

    
    public void mouseExited(MouseEvent e) {glassPane.setVisible(false);}

    public void mouseEntered(MouseEvent e) {glassPane.setVisible(true);}
    
}